package cn.anxingongyou.anxin.controller;

import cn.anxingongyou.anxin.entity.Work;
import cn.anxingongyou.anxin.service.WorkRepository;
import cn.anxingongyou.anxin.service.WorkService;
import cn.anxingongyou.anxin.utils.RedisUtil;
import cn.anxingongyou.anxin.utils.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class WorkController {

    protected static final Logger logger = LoggerFactory.getLogger(WorkController.class);

    private final WorkService workService;
    private final WorkRepository workRepository;
    @Autowired
    public WorkController(WorkService workService, WorkRepository workRepository) {
        this.workService = workService;
        this.workRepository = workRepository;
    }

    @Resource
    private RedisUtil redisUtil;

    @ApiOperation("添加工作信息")
    @ApiResponses({
            @ApiResponse(code = 201,message = "添加成功"),
            @ApiResponse(code = 500,message = "添加失败")
    })
    @RequestMapping(value = "/work/add",method = RequestMethod.POST)
    public Map addWork(
            @RequestBody @ApiParam(value = "工作信息", required = true) Work work
            ){
        work.setCreated(new Date());
        work.setUpdated(new Date());
        int res = workService.insert(work);
        workRepository.save(work);
        if(res > 0) {
            return Result.instance(201,"添加成功").toJsonMap();
        } else {
            return Result.instance(500,"添加失败").toJsonMap();
        }
    }

    @ApiOperation("api添加工作信息")
    @ApiResponses({
            @ApiResponse(code = 201,message = "添加成功"),
            @ApiResponse(code = 500,message = "添加失败")
    })
    @RequestMapping(value = "/api/work/add",method = RequestMethod.POST)
    public Map apiAddWork(
            @RequestBody @ApiParam(value = "工作信息", required = true) Work work
    ){
        work.setCreated(new Date());
        work.setUpdated(new Date());
        int res = workService.insert(work);
        workRepository.save(work);
        if(res > 0) {
            return Result.instance(201,"添加成功").toJsonMap();
        } else {
            return Result.instance(500,"添加失败").toJsonMap();
        }
    }

    @ApiOperation("删除工作信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "删除成功"),
            @ApiResponse(code = 500,message = "删除失败")
    })
    @RequestMapping(value = "/work/del/{id}",method = RequestMethod.GET)
    public Map delWork(
            @PathVariable @ApiParam(value = "工作主键", required = true) Long id
    ){
        String key = "work#"+ id.toString();
        if(redisUtil.hasKey(key)){
            // 已经缓存 删除缓存
            redisUtil.del(key);
        }
        int res = workService.deleteByPrimaryKey(id);
        workRepository.deleteById(id);
        if(res > 0) {
            return Result.instance(204,"删除成功").toJsonMap();
        } else {
            return Result.instance(500,"删除失败").toJsonMap();
        }
    }

    @ApiOperation("更新工作信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/work/update",method = RequestMethod.POST)
    public Map updateWork(
            @RequestBody @ApiParam(value = "工作信息,必传主键", required = true) Work work
    ){
        String key = "work#"+ work.getId().toString();
        if(redisUtil.hasKey(key)){
            // 已经缓存 删除缓存
            redisUtil.del(key);
        }
        int res = workService.updateByPrimaryKeySelective(work);
        Work work1 = workService.selectOneByPrimaryKey(work.getId());
        workRepository.save(work1);
        if(res > 0) {
            return Result.instance(204,"更新成功").toJsonMap();
        } else {
            return Result.instance(500,"更新失败").toJsonMap();
        }
    }

    @ApiOperation("api个人中心更新工作信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/api/work/update",method = RequestMethod.POST)
    public Map apiUpdateWork(
            @RequestBody @ApiParam(value = "工作信息,必传主键", required = true) Work work
    ){
        String key = "work#"+ work.getId().toString();
        if(redisUtil.hasKey(key)){
            // 已经缓存 删除缓存
            redisUtil.del(key);
        }
        int res = workService.updateByPrimaryKeySelective(work);
        Work work1 = workService.selectOneByPrimaryKey(work.getId());
        workRepository.save(work1);
        if(res > 0) {
            return Result.instance(204,"更新成功").toJsonMap();
        } else {
            return Result.instance(500,"更新失败").toJsonMap();
        }
    }

    @ApiOperation("获取单个信息通过某字段")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/work/findByWork",method = RequestMethod.POST)
    public Map findOneWork(
            @RequestBody @ApiParam(value = "工作信息", required = true) Work work
    ){
        Work work1 = workService.selectOne(work);
        if(work1 != null) {
            return Result.<Work>instance(200,"获取成功",work1).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("获取工作信息通过主键")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/work/findOneById/{id}",method = RequestMethod.GET)
    public Map findOneById(
            @PathVariable @ApiParam(value = "报名主键", required = true) Long id
    ){
        Work work = workService.selectOneByPrimaryKey(id);
        if(work != null) {
//            workRepository.save(work);
            return Result.<Work>instance(200,"获取成功",work).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("api获取工作信息通过主键")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/api/work/findOneById/{id}",method = RequestMethod.GET)
    public Map apiFindOneById(
            @PathVariable @ApiParam(value = "报名主键", required = true) Long id
    ){
        String key = "work#"+ id.toString();
        if(redisUtil.hasKey(key)){
            // redis 缓存的该工作详情
            Work work = (Work) redisUtil.get(key);
            if(work != null) {
                return Result.<Work>instance(200,"获取成功",work).toJsonMap();
            } else {
                return Result.instance(500,"获取失败").toJsonMap();
            }
        } else {
            // redis 没缓存的该工作详情
            Work work = workService.selectOneByPrimaryKey(id);
            if(work != null) {
                redisUtil.set(key,work,24*60*60);
                return Result.<Work>instance(200,"获取成功",work).toJsonMap();
            } else {
                return Result.instance(500,"获取失败").toJsonMap();
            }
        }

    }

    @ApiOperation("求职者获取工作")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/work/page/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map selectAddressByWork(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "工作信息", required = true) Work work){
        PageInfo<Work> pageInfo = workService.page(pageNum, pageSize, work);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }

    @ApiOperation("模糊查询求职者工作")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/work/search/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map selectWorkByLike(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "工作信息", required = true) Work work){
        PageInfo<Work> pageInfo = workService.search(pageNum, pageSize, work);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }

    @ApiOperation("api求职者通过条件筛选工作")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/api/work/page/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map selectByWorkInfo(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "工作信息", required = true) Work work){
        PageInfo<Work> pageInfo = workService.page(pageNum, pageSize, work);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }

    @ApiOperation("首页求职者根据经纬度获取范围内的工作,注意需要经纬度和当前时间戳")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/api/work/range/{rangeLong}/{regionLati}/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map selectWorkByLongitudeAndLatitude(
            @PathVariable("rangeLong") @ApiParam(value = "经度上下浮动范围", required = true) BigDecimal rangeLong,
            @PathVariable("regionLati") @ApiParam(value = "纬度上下浮动范围", required = true) BigDecimal regionLati,
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "工作信息", required = true) Work work
            ){
        logger.info("经度上下浮动范围:{}",rangeLong);
        logger.info("纬度上下浮动范围:{}",regionLati);
        PageInfo<Work> pageInfo = workService.range(pageNum,pageSize,rangeLong,regionLati,work);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }

}
